#!/data/data/com.termux/files/usr/bin/sh

# 颜色定义
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
CYAN="\033[36m"
RESET="\033[0m"

show_header() {
    clear
    echo -e "${CYAN}"
    echo "=========================================="
    echo " ZeroTermux 常用配置 - MTHUGO"
    echo " 一键部署 ZeroTermux 本地服务端环境"
    echo "=========================================="
    echo -e "${RESET}"
    
    # 权限提示框
    echo -e "\n${YELLOW}❗❗ 重要提示 ❗❗"
    echo "========================================"
    echo " 即将请求存储权限，请务必注意："
    echo "1. 稍后会弹出权限请求对话框"
    echo "2. 必须点击允许才能正常使用"
    echo "3. 如果误点拒绝，请重新运行本脚本"
    echo -e "========================================${RESET}\n"
}

install_pkg() {
    local pkg_name=$1
    if command -v $pkg_name &>/dev/null; then
        echo -e "${BLUE}[√] ${pkg_name} 已安装${RESET}"
    else
        echo -e "${YELLOW}[▶] 正在安装 ${pkg_name}...${RESET}"
        pkg install $pkg_name -y >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}[✔] ${pkg_name} 安装成功${RESET}"
        else
            echo -e "${RED}[✘] ${pkg_name} 安装失败${RESET}"
        fi
    fi
}

# 显示引导界面
show_header

# ==================== 阶段1: 存储权限 ====================
echo -e "\n${CYAN}〔阶段1〕存储权限配置${RESET}"
echo -e "${YELLOW}▶ 正在请求存储权限...${RESET}"
termux-setup-storage
echo -e "${GREEN}✔ 权限请求已完成，请确认您已允许访问存储${RESET}"
sleep 1

# ==================== 阶段2: 软件源配置 ====================
echo -e "\n${CYAN}〔阶段2〕软件源配置${RESET}"
echo -e "${YELLOW}▶ 正在替换为清华源...${RESET}"
sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
sed -i 's@^\(deb.*games stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/game-packages-24 games stable@' $PREFIX/etc/apt/sources.list.d/game.list
sed -i 's@^\(deb.*science stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/science.list
echo -e "${GREEN}✔ 软件源配置完成${RESET}"

# ==================== 阶段3: 系统升级 ====================
echo -e "\n${CYAN}〔阶段3〕系统升级${RESET}"
echo -e "${YELLOW}▶ 正在更新软件包列表...${RESET}"
yes | pkg update >/dev/null 2>&1
echo -e "${YELLOW}▶ 正在升级系统组件...${RESET}"
yes | pkg upgrade >/dev/null 2>&1
echo -e "${GREEN}✔ 系统升级完成${RESET}"

# ==================== 阶段4: 基础工具安装 ====================
echo -e "\n${CYAN}〔阶段4〕基础工具安装${RESET}"
# zip安装
echo -e "${YELLOW}▶ 正在更新并安装zip...${RESET}"
yes | pkg update >/dev/null 2>&1
yes | pkg install zip -y >/dev/null 2>&1

install_pkg wget
install_pkg git
install_pkg unzip
install_pkg proot
install_pkg openssl-tool

# 确保which命令可用
if ! command -v which &>/dev/null; then
    echo -e "${YELLOW}[▶] 正在安装which命令依赖...${RESET}"
    pkg install debianutils -y >/dev/null 2>&1
fi

# ==================== 阶段5: Python环境 ====================
echo -e "\n${CYAN}〔阶段5〕Python环境配置${RESET}"
install_pkg python3
install_pkg python-pip
echo -e "${YELLOW}▶ 正在安装Flask...${RESET}"
pip3 install flask >/dev/null 2>&1 && echo -e "${GREEN}✔ Flask安装成功${RESET}"

# ==================== 阶段6: Java环境 ====================
echo -e "\n${CYAN}〔阶段6〕Java环境配置${RESET}"
install_pkg openjdk-17

# ==================== 阶段7: APK工具链安装 ====================
echo -e "\n${CYAN}〔阶段7〕APK工具链安装${RESET}"
APKTOOL_PATH="/data/data/com.termux/files/usr/bin/apktool"
APKTOOL_JAR_PATH="/data/data/com.termux/files/usr/bin/apktool.jar"

# 安装apktool主程序
if [ ! -f "$APKTOOL_PATH" ]; then
    echo -e "${YELLOW}▶ 正在安装apktool...${RESET}"
    if wget -q https://gitee.com/jianxingjiany/cloud-based-files/raw/master/apktool -O "$APKTOOL_PATH"; then
        chmod +x "$APKTOOL_PATH"
        echo -e "${GREEN}✔ apktool安装成功${RESET}"
    else
        echo -e "${RED}✘ apktool下载失败，请检查网络连接${RESET}"
    fi
else
    echo -e "${BLUE}[√] apktool已存在${RESET}"
fi

# 安装apktool.jar
if [ ! -f "$APKTOOL_JAR_PATH" ]; then
    echo -e "${YELLOW}▶ 正在安装apktool.jar...${RESET}"
    if wget -q https://bitbucket.org/iBotPeaches/apktool/downloads/apktool_2.11.1.jar -O "$APKTOOL_JAR_PATH"; then
        chmod +x "$APKTOOL_JAR_PATH"
        echo -e "${GREEN}✔ apktool.jar安装成功${RESET}"
    else
        echo -e "${RED}✘ apktool.jar下载失败，请检查网络连接${RESET}"
    fi
else
    echo -e "${BLUE}[√] apktool.jar已存在${RESET}"
fi

# 安装aapt并验证
if ! command -v aapt &>/dev/null; then
    echo -e "${YELLOW}▶ 正在安装aapt...${RESET}"
    if pkg install aapt -y >/dev/null 2>&1; then
        echo -e "${GREEN}✔ aapt安装成功"
        # 通用版本检测方法
        aapt_ver=$(aapt 2>&1 | head -n 1 | grep -oP 'v\d+\.\d+\.\d+')
        echo -e "版本信息：${aapt_ver}${RESET}"
    else
        echo -e "${RED}✘ aapt安装失败，请手动执行：pkg install aapt${RESET}"
    fi
else
    aapt_ver=$(aapt 2>&1 | head -n 1 | grep -oP 'v\d+\.\d+\.\d+')
    echo -e "${BLUE}[√] aapt已安装"
    echo -e "当前版本：${aapt_ver}${RESET}"
fi

# ==================== 阶段8: 环境优化 ====================
echo -e "\n${CYAN}〔阶段8〕环境优化${RESET}"
echo 'export PATH="$PATH:/data/data/com.termux/files/usr/bin"' >> ~/.bashrc
source ~/.bashrc

# 完成提示
echo -e "\n${GREEN}========================================"
echo "✅ 所有配置已完成！"
echo "========================================"
echo -e "${CYAN}验证命令："
echo -e "${YELLOW}1. apktool --version    ${CYAN}# 反编译工具验证"
echo -e "${YELLOW}2. python3 -V          ${CYAN}# Python版本验证"
echo -e "${YELLOW}3. java -version       ${CYAN}# Java环境验证"
echo -e "${YELLOW}4. aapt version        ${CYAN}# 资源打包工具验证"
echo -e "${YELLOW}5. zip --version       ${CYAN}# 压缩工具验证"
echo -e "${RESET}"
